package com.movitech.mobile.cz.base.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movitech.mobile.cz.base.common.license.LicenseUtil;
import com.movitech.mobile.cz.base.common.util.StringUtils;
import com.movitech.mobile.cz.base.sys.entity.SRegionalCompany;
import com.movitech.mobile.cz.base.sys.entity.ext.SCompanyRequestVO;
import com.movitech.mobile.cz.base.sys.entity.SCompany;
import com.movitech.mobile.cz.base.sys.mapper.SCompanyMapper;
import com.movitech.mobile.cz.base.sys.mapper.ext.ExtSCompanyMapper;
import com.movitech.mobile.cz.base.sys.service.SCompanyService;
import com.movitech.mobile.cz.base.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 公司 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-04-11
 */
@Service("sCompanyService")
public class SCompanyServiceImpl extends ServiceImpl<SCompanyMapper, SCompany> implements SCompanyService {

    @Autowired
    private ExtSCompanyMapper extSCompanyMapper;
    @Autowired
    private SRegionalCompanyServiceImpl sRegionalCompanyService;

    @Override
    public List<SCompany> getSCompanyByCodeAndRegionalCompanyId(String code, String regionalCompanyId) {
        SCompany searchSCompany = new SCompany();
        searchSCompany.setCompanyCode(code);
        if (StringUtils.isNotEmpty(regionalCompanyId)) searchSCompany.setRegionalCompanyId(regionalCompanyId);
        EntityWrapper<SCompany> wrapper = new EntityWrapper<SCompany>();
        wrapper.setEntity(searchSCompany);
        return selectList(wrapper);
    }

    public List<SCompany> getSCompanyListByCodes(List<String> lstCodes) {
        Map<String, Object> mapParams = new HashMap<String, Object>();
        mapParams.put("lstCompanyCode", lstCodes);
        return extSCompanyMapper.getSCompanyListByCodes(mapParams);
    }

    @Override
    public SCompany selectByCode(String code) {
        return extSCompanyMapper.selectByCode(code);
    }

    @Override
    public SCompany selectByProjectFatherCode(String fatherCode) {
        LicenseUtil.INSTANCE.validate();
        return extSCompanyMapper.selectByProjectFatherCode(fatherCode);
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackForClassName = {"RuntimeException", "Exception"})
    @Override
    public void batchAdd(List<SCompanyRequestVO> scompanyList) {
        LicenseUtil.INSTANCE.validate();
        try {
            if (scompanyList != null && scompanyList.size() > 0) {
                for (SCompanyRequestVO sCompanyRequestVO : scompanyList) {
                    if (StringUtils.isEmpty(sCompanyRequestVO.getRemarks())) {
                        continue;
                    }
//                    List<SRegionalCompany> sRegionalCompanyList = sRegionalCompanyService.getRegionalCompanyByCode(sCompanyRequestVO.getRegionalCompanyId());
                    SCompany scompany = this.selectByCode(sCompanyRequestVO.getRemarks());
                    if (scompany == null) {
                        scompany = new SCompany();
                        scompany.setCompanyCode(sCompanyRequestVO.getCompanyCode());
                        scompany.setFullCode(sCompanyRequestVO.getFullCode());
                        scompany.setCompanyName(sCompanyRequestVO.getCompanyName());
                        scompany.setRemarks(sCompanyRequestVO.getRemarks());
                       /* if (sRegionalCompanyList != null && sRegionalCompanyList.size() > 0) {
                            scompany.setRegionalCompanyId(sRegionalCompanyList.get(0).getId());
                        }*/
                        scompany.setBaseFeilds(UserUtils.getCurrentUserId(), "", sCompanyRequestVO.getStatus(), true);
                        this.insert(scompany);
                    } else {
                        if (scompany.getCompanyCode().equals(sCompanyRequestVO.getCompanyCode())) {
                            scompany.setCompanyName(sCompanyRequestVO.getCompanyName());
                            scompany.setFullCode(sCompanyRequestVO.getFullCode());
                            scompany.setRemarks(sCompanyRequestVO.getRemarks());
                          /*  if (sRegionalCompanyList != null && sRegionalCompanyList.size() > 0) {
                                scompany.setRegionalCompanyId(sRegionalCompanyList.get(0).getId());
                            }*/
                            scompany.setBaseFeilds(UserUtils.getCurrentUserId(), "", sCompanyRequestVO.getStatus(), false);
                            this.updateById(scompany);
                        }
                    }
                }
                // 整体更新区域
                // 1、更新非腾云筑科平台的下属公司
                this.updateRegionCompanyId4Root();
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    private void updateRegionCompanyId4Root() {
        LicenseUtil.INSTANCE.validate();
        extSCompanyMapper.updateRegionCompanyId4Other();
        extSCompanyMapper.updateRegionCompanyId4Root();
    }
}
